package com.zhuozhou.jdyc.util;

import android.content.Context;
import android.hardware.fingerprint.FingerprintManager;
import android.util.Log;

import androidx.core.hardware.fingerprint.FingerprintManagerCompat;
import androidx.core.os.CancellationSignal;


/**
 * Created by Chuxy on 2020/4/7
 */
public class FingerLock {
    private FingerprintManagerCompat mFingerprintManagerCompat;
    private CancellationSignal mCancellationSignal;
    private Context context;

    public FingerLock(Context context) {
        this.context = context;
        if (mFingerprintManagerCompat == null || mCancellationSignal == null) {
            mFingerprintManagerCompat = FingerprintManagerCompat.from(context);
            mCancellationSignal = new CancellationSignal();
        }
    }

    public interface FingerPrintResult {
        void success();

        void error(int code, CharSequence info);

        void retry(int code, CharSequence info);
    }

    /**
     * 关闭
     *
     * @return
     */
    public boolean cancelCallback() {
        if (mCancellationSignal != null && !mCancellationSignal.isCanceled()) {
            mCancellationSignal.cancel();
            mCancellationSignal = null;
            return true;
        } else {
            return false;
        }
    }

    /**
     * 开始
     */
    public void start(final FingerPrintResult fpResult) {
        if (fpResult == null) {
            throw new RuntimeException("出现错误，请稍后再试！");
        }
        if (mFingerprintManagerCompat.isHardwareDetected() && mFingerprintManagerCompat.hasEnrolledFingerprints()) {
            mFingerprintManagerCompat.authenticate(null, 0, mCancellationSignal, new FingerprintManagerCompat.AuthenticationCallback() {
                            @Override
                            public void onAuthenticationError(int errorCode, CharSequence errString) {
                                Log.d("-=-", "onAuthenticationError" + errString);
                                super.onAuthenticationError(errorCode, errString);
                                if (errorCode == FingerprintManager.FINGERPRINT_ERROR_LOCKOUT) {
                                    fpResult.error(-1, errString);
                                } else {
                                    fpResult.retry(errorCode, errString);
                                }
                            }

                            @Override
                            public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
                                super.onAuthenticationHelp(helpCode, helpString);
                                Log.d("-=-", "onAuthenticationHelp" + helpString);
                                fpResult.retry(helpCode, helpString);
                            }

                            @Override
                            public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
                                super.onAuthenticationSucceeded(result);
                                Log.d("-=-", "onAuthenticationSucceeded" + result);
                                fpResult.success();
                            }

                            @Override
                            public void onAuthenticationFailed() {
                                super.onAuthenticationFailed();
                                Log.d("-=-", "onAuthenticationFailed");
                                fpResult.retry(0, "验证失败，请重试！");
                            }
                        }, null);
        } else {
            fpResult.error(-1, "出现错误，请稍后再试！");

        }
    }
}
